'''
Created on 2019年6月18日

@author: Administrator
'''

import logging
import logging.handlers
import os
import threading

from jenkins_tool.common import const
import jenkins_tool.common.SingleTon as Singleton

@Singleton.Singleton
class Logger:

    config_dir = const.Log_config_dir
    config_file_name = const.Log_config_file_name
    
    
    def __init__(self, dir_path=config_dir,file_name=config_file_name,clevel = logging.INFO,Flevel = logging.DEBUG):

        if (os.path.exists(dir_path)) == False:
            os.makedirs(dir_path)
        self.logger = logging.getLogger(dir_path+'/' + file_name)
        self.logger.setLevel(logging.DEBUG)
        fmt = logging.Formatter('[%(asctime)s.%(msecs)03d] [%(levelname)s] %(message)s',
                                '%Y-%m-%d %H:%M:%S')

        #设置CMD日志
        sh = logging.StreamHandler()
        sh.setFormatter(fmt) 
        sh.setLevel(clevel)
        #设置文件日志
        # fh = logging.FileHandler(dir_path+'/' + file_name)
        # fh.setFormatter(fmt)
        # fh.setLevel(Flevel)

        # init time rotation filehandler which will clean the logfile in certain frequencies
        th = logging.handlers.TimedRotatingFileHandler(dir_path + '/' + file_name,
                                                       when="D", interval=1, encoding='utf-8', backupCount=1000)
        th.setFormatter(fmt)
        th.setLevel(Flevel)

        for hdl in self.logger.handlers[:]:
            self.logger.removeHandler(hdl)

        self.logger.addHandler(sh)
        self.logger.addHandler(th)
        # self.logger.addHandler(fh)

    def debug(self,class_name,method_name,message, sys_id, 
              thread_id=threading.currentThread().ident,thread_name=threading.currentThread().name):
        self.logger.debug('[' +class_name+ ']' + '[' + method_name+']'+'['+str(sys_id)+ ']' +'['+str(thread_id)+']'+'['+thread_name+']'+' message: ' +message)
    
    
    def info(self,class_name,method_name,message,sys_id,
              thread_id=threading.currentThread().ident,thread_name=threading.currentThread().name):
        # print(self)
        self.logger.info('[' +class_name+ ']' + '[' + method_name+']'+'['+str(sys_id)+ ']'+'['+str(thread_id)+']'+'['+thread_name+']'+' message: ' +message)
    
    def war(self,class_name,method_name,message,sys_id,
              thread_id=threading.currentThread().ident,thread_name=threading.currentThread().name):
        self.logger.warnning('[' +class_name+ ']' + '[' + method_name+']'+'['+str(sys_id)+ ']'+'['+str(thread_id)+']'+'['+thread_name+']'+' message: ' +message)

    def error(self,class_name,method_name,message,sys_id,
              thread_id=threading.currentThread().ident,thread_name=threading.currentThread().name):
        self.logger.error('[' +class_name+ ']' + '[' + method_name+']'+'['+str(sys_id)+ ']'+'['+str(thread_id)+']'+'['+thread_name+']'+' message: ' +message)

    def cri(self,class_name,method_name,message,sys_id,
              thread_id=threading.currentThread().ident,thread_name=threading.currentThread().name):
        self.logger.critical('[' +class_name+ ']' + '[' + method_name+']'+'['+str(sys_id)+ ']'+'['+str(thread_id)+']'+'['+thread_name+']'+' message: ' +message)

# if __name__ =='__main__':
#     config_dir = 'log/pylog/' 
#     config_file_name = 'naushika.log'
# #     print(os._exists(os.path.exists(config_dir)))
# #     if os._exists(os.path.exists(config_dir)) == False:
# #         os.makedirs(config_dir)
#     
#     logyyx = Logger(clevel = logging.DEBUG,Flevel = logging.DEBUG)
#     logyyx.debug(logyyx.__class__.__name__,sys._getframe().f_code.co_name,'一个debug信息')
#     
#     print(threading.currentThread().ident )
#     print(threading.currentThread().name)
